import sys
import os
import random
from datetime import datetime
from passlib.hash import bcrypt
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from sqlalchemy.orm import Session
from app.database import SessionLocal, engine
from app.models.database import Base, Building, Room, Student, Preference, Admin

# 创建所有表
Base.metadata.create_all(bind=engine)

def create_test_data():
    db = SessionLocal()
    try:
        # 创建宿舍楼
        buildings = [
            Building(name="1号楼", floors=6),
            Building(name="2号楼", floors=6),
            Building(name="3号楼", floors=6)
        ]
        db.add_all(buildings)
        db.commit()

        # 为每栋楼创建房间
        rooms = []
        for building in buildings:
            for floor in range(1, building.floors + 1):
                for room_num in range(1, 11):  # 每层10个房间
                    room_number = f"{floor}0{room_num}" if room_num < 10 else f"{floor}{room_num}"
                    rooms.append(Room(
                        building_id=building.building_id,
                        room_number=room_number,
                        capacity=4,
                        current_num=0
                    ))
        db.add_all(rooms)
        db.commit()

        # 创建测试学生数据
        colleges = ["计算机学院", "机械工程学院", "电子工程学院", "经济管理学院"]
        hobbies = ["游戏", "运动", "阅读", "音乐", "电影", "旅行", "摄影", "编程", "绘画", "烹饪"]
        
        students = []
        preferences = []
        
        # 生成100个测试学生
        for i in range(1, 101):
            student_id = f"2024{str(i).zfill(4)}"
            gender = "M" if i % 2 == 0 else "F"
            
            student = Student(
                student_id=student_id,
                password=bcrypt.hash("password123"),
                name=f"测试学生{i}",
                gender=gender,
                college=random.choice(colleges),
                contact=f"1{str(random.randint(3, 9))}{str(random.randint(100000000, 999999999))}",
                created_at=datetime.now()
            )
            students.append(student)
            
            # 为每个学生创建偏好
            preference = Preference(
                student_id=student_id,
                sleep_time=random.choice(["早睡", "正常", "熬夜"]),
                cleanliness=random.randint(1, 5),
                hobby_tags=random.sample(hobbies, random.randint(2, 5))
            )
            preferences.append(preference)
        
        db.add_all(students)
        db.commit()
        db.add_all(preferences)
        db.commit()

        # 创建管理员
        admin = Admin(
            username="admin",
            password=bcrypt.hash("admin123"),
            name="系统管理员",
            role="super_admin",
            created_at=datetime.now()
        )
        db.add(admin)
        db.commit()

        print("测试数据创建成功！")
        print(f"创建了 {len(buildings)} 栋宿舍楼")
        print(f"创建了 {len(rooms)} 个房间")
        print(f"创建了 {len(students)} 个学生账号")
        print(f"创建了 {len(preferences)} 条偏好记录")
        print("管理员账号：admin")
        print("管理员密码：admin123")
        print("所有学生默认密码：password123")

    except Exception as e:
        print(f"创建测试数据时出错：{str(e)}")
        db.rollback()
    finally:
        db.close()

if __name__ == "__main__":
    create_test_data() 